package com.yuyou.fn.controller;

import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.platform.service.ISysUserService;
import com.yuyou.fn.platform.vo.AccessTokenVo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wens on 2017/2/22.
 */
public class AdminApiBaseController {

    @Resource
    protected ISysUserService sysUserService ;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    protected Long getUserId(HttpServletRequest request) {
        AccessTokenVo accessToken = (AccessTokenVo)request.getAttribute("accessToken");
        Assert.notNull(accessToken);
        return accessToken.getUserId();
    }

    protected SysUser getUser(HttpServletRequest request) {
        return sysUserService.findById(getUserId(request));
    }

}
